:- import "emitter.sl" #
:- import "frame.sl" #
:- import "generator.sl" #
:- import "operands.sl" #
:- import "parser.sl" #
:- import "registers.sl" #
:- import "types.sl" #
:- import "%%/lib/scanner.sl" #

-- Compilation flow:
--
-- - (parser.sl) Parse
-- - (environment.sl) Construct namespace
-- - (checker.sl) Semantics check
--     Type checking
-- - (frame.sl)
--     Stack frames extraction
--     Replaces variables with generated addresses
-- - (type.sl)
--     Replace typenames with real types
--     Replace member accesses to REL
-- - (ssa.sl) Conversion to static single assignment
-- - (generator.sl) Register assignment
-- - (emitter.sl) Generate instruction

Compile .filename :-

	Parse.File .filename .tree,

	nl, write "PROGRAM -", nl, dump .tree, nl,

	once (
		TypeName.Derive (.tree => .tree1);
		Error "Internal type resolve error"
	),

--	nl, write "TYPE DERIVED -", nl, dump .tree, nl,

	once (
		Frame.Derive (.tree1 => .tree2);
		Error "Internal frame generation error"
	),

	nl, write "OUTPUT -", nl, stream.stdout OUT.ASM.FILE, set OUTPUT OUT.ASM.FILE,

	once (
		Generate .tree2;
		Error "Internal generator error"
	),

	Emit.Flush,
	stream.close OUT.ASM.FILE, unset OUTPUT #

Error .message :-
	scanner.currentLine (parser.scanner (get PARSER ^) ^) .line,
	write "LINE" .line "ERROR:" .message, nl ! #
Error . #
